package com.example.springboot_demo.controller;

import com.example.springboot_demo.aop.annotation.Log;
import com.example.springboot_demo.dto.ClazzRoom;
import com.example.springboot_demo.dto.User;
import com.example.springboot_demo.exception.SpringBootDemoException;
import com.example.springboot_demo.utils.AjaxResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description: 处理 类似于这样的请求：#https://le3-api.game.bilibili.com/pc/game/nav/info?id=1&name=xm
 * 1.query .../info?id=1&name=xm get post delete put
 * 2.表单提交  post delete put
 * 3.json post delete put
 * <P> Date: 2024/6/21:20:03
 */

@SuppressWarnings({"unchecked", ""})
@RequestMapping("/form")
//@Controller
//@ResponseBody
@RestController
@Validated
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);

    /**
     * 接收表单传参
    */
    @RequestMapping("/StringValue")
    public Object stringValue(@RequestParam("name1") String name, String password){
        log.info("name:{}",name);
        log.info("password:{}",password);
        return "ok";
    }

    /**
     * query 传参
     * {@link FormController#queryParams(java.lang.String, java.lang.String)}
    */
    @RequestMapping("/queryParams")
    public Object queryParams(@RequestParam("name1") String name, String password){
        log.info("name:{}",name);
        log.info("password:{}",password);
        return "ok";
    }
    /**
     * Arrays
    */

    /**
     *接收前端form提交的数组
     * @param ids
     */
    @RequestMapping(value="/arrays",method ={RequestMethod.GET,RequestMethod.POST} )
    public Object arrays(@RequestParam List<Integer> ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 传入一堆数据，后端自动封装成Map集合
     * @param user
     */
    @ResponseBody
    @RequestMapping("/map")
    public Map receiveParameters(@RequestParam Map<String,Object> user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     *接收前端 json 提交的数组
     * @param list2Json
     */
    @RequestMapping("/arrays2Json")
    public Object arrays2Json(@RequestBody List2Json list2Json){
        Map<Object, Object> hashMap = new HashMap<>(2);
        hashMap.put("data",list2Json);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     *  传入一堆数据，后端自动封装成User对象
     * @param user
     */
    @ResponseBody
    @RequestMapping("/user")
    public Map receiveParameters(User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }


    @Data
    public static class List2Json{
        private List<Integer> ids;
        private String name;
    }


    /**
     * result ful
     * findByIdAndName
    */
    @GetMapping("/findByIdAndName/{id}/{name}")
    public Map findByIdAndName (@PathVariable("id") Integer id,@PathVariable("name") String name ){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("id",id);
        hashMap.put("name",name);
        return hashMap;
    }

    @PostMapping("/checkUser")
    public Map checkUser( @RequestBody @Validated User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 普通类型检查
    */
    @GetMapping("/checkMethodParam")
    public AjaxResult checkMethodParam(@NotBlank(message = "name 参数不能为空") String name,
                                       Integer id){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("name",name);
        hashMap.put("id",id);
        return AjaxResult.success(hashMap);
    }

    /**
     * 捕获自定义异常
    */
    @GetMapping("/throwMyException")
    @Log
    public AjaxResult throwMyException(Integer num){
        if(num > 10){
            throw new SpringBootDemoException("手动抛出业务异常");
        }
        return AjaxResult.success();
    }

    /**
     * 抛出 除0异常
     */
    @Log
    @GetMapping("/throwException")
    public AjaxResult throwException(Integer num){
        int i = 1/0;
        return AjaxResult.success();
    }


    /**
     * 使用表单或者query 传时间参数
     */
    @GetMapping("/dataFormat2Form")
    public Map dataFormat2Form(User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 使用json  传时间参数
     */
    @GetMapping("/dataFormat2Obj")
    public Map dataFormat2Obj(@RequestBody User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 传入一堆数据，后端自动封装成Map集合
     * @param user
     */
    @ResponseBody
    @RequestMapping("/user")
    public Map objParameters( User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 复合对象数据传输
     * @param clazzRoom
     */
    @PostMapping("/ClazzUserValue")
    @ResponseBody
    public Map<Object, Object> receiveParameters(ClazzRoom clazzRoom){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 嵌套对象 使用json传输
     * @param clazzRoom
     */
    @PostMapping("/ClazzUserValue2Json")
    @ResponseBody
    public Map<Object, Object> clazzUserValue2Json(@RequestBody ClazzRoom clazzRoom){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }


}
